import * as types from '../action-typess';
import { getHomeData, getCateData, getDetail, getMoreHot, getPrize } from '../../api/home';

export function fetchHome() {
  return function(dispatch,getState) {
    dispatch({type: types.FETCH_HOME});
    let {page} = getState().homepage;
    getHomeData(page).then(payload=>{
      dispatch({type: types.FETCH_HOME_FULFILLED, payload: payload});
    })
  }
}

export function fetchCate(cid) {
  return function(dispatch) {
    dispatch({type: types.FETCH_CATE});
    getCateData(cid).then(payload=>{
      dispatch({type: types.FETCH_CATE_FULFILLED, payload: payload});
    })
  }
}

export function clearDetail() {
  return function(dispatch) {
    dispatch({type: types.CLEAR_DETAIL});
  }
}

export function fetchDetail(id) {
  return function(dispatch) {
    dispatch({type: types.FETCH_DETAIL});
    getDetail(id).then(payload=>{
      dispatch({type: types.FETCH_DETAIL_FULFILLED, payload: payload});
    })
  }
}

export function fetchMoreHot() {
  return function(dispatch) {
    dispatch({type: types.FETCH_MOREHOT});
    getMoreHot().then(payload=>{
      dispatch({type: types.FETCH_MOREHOT_FULFILLED, payload: payload});
    })
  }
}

export function onPrizeData(payload) {
  return function(dispatch) {
    dispatch({type: types.FETCH_PRIZE_FULFILLED, payload: payload});
  }
}

export function fetchPrize(id) {
  return function(dispatch) {
    dispatch({type: types.FETCH_PRIZE});
    getPrize(id).then(payload=>{
      dispatch({type: types.FETCH_PRIZE_FULFILLED, payload: payload});
    })
  }
}